package com.sita.tboard.ui.fragment;

import android.app.Activity;
import android.app.Fragment;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sita.bike.R;
import com.sita.bike.persistence.RouteData;
import com.sita.bike.utils.PersistUtils;

import java.util.ArrayList;
import java.util.List;

import lecho.lib.hellocharts.formatter.PieChartValueFormatter;
import lecho.lib.hellocharts.model.Axis;
import lecho.lib.hellocharts.model.Line;
import lecho.lib.hellocharts.model.LineChartData;
import lecho.lib.hellocharts.model.PieChartData;
import lecho.lib.hellocharts.model.PointValue;
import lecho.lib.hellocharts.model.SliceValue;
import lecho.lib.hellocharts.model.ValueShape;
import lecho.lib.hellocharts.util.ChartUtils;
import lecho.lib.hellocharts.view.LineChartView;
import lecho.lib.hellocharts.view.PieChartView;

public class ComboPicLineChartFragment extends Fragment {
    public static final int DeepSkyBlue = Color.parseColor("#aed8f1");
    public static final int PaleturQuoise = Color.parseColor("#78bbe7");
    public static final int PowderBlue = Color.parseColor("#3498db");
    public PieChartView pieChartView;
    public LineChartView lineChartView;
    float[] randomNumbersTab;
    private PieChartData data;
    private boolean hasLabels = false;
    private boolean hasLabelsOutside = false;
    private boolean hasCenterCircle = false;
    private boolean hasCenterText1 = false;
    private boolean hasCenterText2 = false;
    private boolean isExploded = false;
    private boolean hasLabelForSelected = false;
    private LineChartData datas;
    private int numberOfPoints = 0;
    private boolean hasAxes = true;
    private boolean hasAxesNames = true;
    private boolean hasLines = true;
    private boolean hasPoints = true;
    private ValueShape shape = ValueShape.CIRCLE;
    private boolean isFilled = false;
    private boolean hasLabel = false;
    private boolean isCubic = false;
    private boolean hasLabelForSelecte = false;
    private boolean pointsHaveDifferentColor;
    private long tracksID;

    public ComboPicLineChartFragment() {
        // Required empty public constructor
    }

    public static ComboPicLineChartFragment newInstance(long tracksID) {
        ComboPicLineChartFragment fragment = new ComboPicLineChartFragment();
        Bundle bundle = new Bundle();
        bundle.putLong("tracksID", tracksID);
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_combo_pic_line_chart, container, false);
        tracksID = getArguments().getLong("tracksID");
        initView(view);
        generatePicData();
//        List<PointValue> mPointValues = new ArrayList<>();
//        for (int i = 0; i < 10 ; i++) {
//             mPointValues.add(new PointValue(i, new Random().nextInt(10)));
////            mAxisValues.add(new AxisValue(i).setLabel(i)); //为每个对应的i设置相应的label(显示在X轴)
//            }
//        Line line = new Line(mPointValues).setColor(R.color.blue).setCubic(false);
//        List<Line> lines = new ArrayList<Line>();
//       lines.add(line);
//        LineChartData data = new LineChartData();
//        data.setLines(lines);
//
//       //坐标轴
//       Axis axisX = new Axis(); //X轴
//       axisX.setHasTiltedLabels(true);
//        axisX.setTextColor(R.color.blue);
//       axisX.setName("采集时间");
//        axisX.setMaxLabelChars(10);
////        axisX.setValues(mAxisValues);
//        data.setAxisXBottom(axisX);
//
//        Axis axisY = new Axis();  //Y轴
//        axisY.setMaxLabelChars(7); //默认是3，只能看最后三个数字
//       data.setAxisYLeft(axisY);
//        lineChartView.setInteractive(true);
//        lineChartView.setZoomType(ZoomType.HORIZONTAL);
//        lineChartView.setContainerScrollEnabled(true, ContainerScrollType.HORIZONTAL);
//        lineChartView.setLineChartData(data);
//        lineChartView.setVisibility(View.VISIBLE);


        generateLineChartValues();
        generateLineChartData();
        return view;
    }

    public void initView(View view) {
        pieChartView = (PieChartView) view.findViewById(R.id.chart);
        lineChartView = (LineChartView) view.findViewById(R.id.chart_preview);
    }

    //饼状图
    public void generatePicData() {
        int numValues = 3;
        SliceValue sliceValue;
        List<SliceValue> values = new ArrayList<>();
        for (int i = 0; i < numValues; ++i) {
            switch (i) {
                case 0:
                    sliceValue = new SliceValue(34, PaleturQuoise);
                    values.add(sliceValue);
                    break;
                case 1:
                    sliceValue = new SliceValue(43, DeepSkyBlue);
                    values.add(sliceValue);
                    break;
                case 2:
                    sliceValue = new SliceValue(33, PowderBlue);
                    values.add(sliceValue);
                    break;
            }

        }

        data = new PieChartData(values);
        data.setFormatter(new PieChartValueFormatter() {
            @Override
            public int formatChartValue(char[] formattedValue, SliceValue value) {
                char[] array = (String.valueOf(value.getValue()) + "%").toCharArray();
                System.arraycopy(array, 0, formattedValue, 0, array.length);
                return formattedValue.length;
            }
        });
        data.setHasLabels(true);
        data.setHasLabelsOnlyForSelected(hasLabelForSelected);
        data.setHasLabelsOutside(hasLabelsOutside);
        data.setHasCenterCircle(hasCenterCircle);
        if (isExploded) {
            data.setSlicesSpacing(24);
        }

        if (hasCenterText1) {
            data.setCenterText1("Hello!");

            // Get roboto-italic font.
            Typeface tf = Typeface.createFromAsset(getActivity().getAssets(), "Roboto-Italic.ttf");
            data.setCenterText1Typeface(tf);

            // Get font size from dimens.xml and convert it to sp(library uses sp values).
            data.setCenterText1FontSize(ChartUtils.px2sp(getResources().getDisplayMetrics().scaledDensity,
                    (int) getResources().getDimension(R.dimen.pie_chart_text1_size)));
        }

        if (hasCenterText2) {
            data.setCenterText2("Charts (Roboto Italic)");

            Typeface tf = Typeface.createFromAsset(getActivity().getAssets(), "Roboto-Italic.ttf");

            data.setCenterText2Typeface(tf);
            data.setCenterText2FontSize(ChartUtils.px2sp(getResources().getDisplayMetrics().scaledDensity,
                    (int) getResources().getDimension(R.dimen.pie_chart_text2_size)));
        }

        pieChartView.setPieChartData(data);
    }

    //添加折线点的数据，
    public void generateLineChartValues() {
        List<RouteData> gps = PersistUtils.getRouteDataList(tracksID);
        numberOfPoints = gps.size();
        randomNumbersTab = new float[numberOfPoints];
        for (int j = 0; j < numberOfPoints; ++j) {
            double spd = gps.get(j).getSpeed() * 3.6;
            Log.e("ComboPic折线点", String.valueOf(spd));
            randomNumbersTab[j] = (float) spd;
        }
    }

    public void generateLineChartData() {
        List<Line> lines = new ArrayList<>();
        List<PointValue> values = new ArrayList<>();
        for (int j = 0; j < numberOfPoints; ++j) {
            values.add(new PointValue(j, randomNumbersTab[j]));
        }
        Line line = new Line(values);
        line.setColor(ChartUtils.COLORS[0]);
        line.setShape(shape);
        line.setCubic(isCubic);
        line.setFilled(isFilled);
        line.setHasLabels(hasLabels);
        line.setHasLabelsOnlyForSelected(hasLabelForSelected);
        line.setHasLines(hasLines);
        line.setHasPoints(hasPoints);
        if (pointsHaveDifferentColor) {
            line.setPointColor(ChartUtils.COLORS[(1) % ChartUtils.COLORS.length]);
        }
        lines.add(line);

        datas = new LineChartData(lines);

        if (hasAxes) {
            Axis axisX = new Axis();
            Axis axisY = new Axis().setHasLines(true);
            if (hasAxesNames) {
                axisX.setName("s");
                axisY.setName("km/h");
            }
            datas.setAxisXBottom(axisX);
            datas.setAxisYLeft(axisY);
        } else {
            datas.setAxisXBottom(null);
            datas.setAxisYLeft(null);
        }

        datas.setBaseValue(Float.NEGATIVE_INFINITY);
        lineChartView.setLineChartData(datas);

    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

    }

    @Override
    public void onDetach() {
        super.onDetach();

    }
}
